package com.sky.autoFill;


import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    //切入点
    @Pointcut(value = "execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.autoFill.AutoFill))")
    public void autoFillPointCut(){}

    /**
     * 通知 自动填充公共字段
     * @param joinpoint
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinpoint){
        log.info("公共字段填充。。");
        //获得方法签名对象
        MethodSignature signature =(MethodSignature) joinpoint.getSignature();
        //获得方法上的注解
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        //解析注解中的操作类型
        OperationType operationType = autoFill.value();

        //获得当前目标方法的参数
        Object[] args = joinpoint.getArgs();
        if (args==null || args.length==0) {
            return;
        }
        //获得实体参数
        Object entity = args[0];

        LocalDateTime localDateTime = LocalDateTime.now();
        Long empId = BaseContext.getCurrentId();

        if (operationType==OperationType.INSERT){
            try {
                //通过反射获得set方法
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                //通过反射调用对象方法
                setCreateUser.invoke(entity,empId);
                setCreateTime.invoke(entity,localDateTime);
                setUpdateUser.invoke(entity,empId);
                setUpdateTime.invoke(entity,localDateTime);
            } catch (Exception e) {
                log.error("公共字段填充失败:{}",e.getMessage());
            }
        }else if(operationType==OperationType.UPDATE){
            //当前为更改，为两个值赋值
            try {
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                setUpdateUser.invoke(entity, empId);
                setUpdateTime.invoke(entity,localDateTime);
            } catch (Exception e) {
                log.error("公共字段填充失败：{}",e.getMessage());
            }
        }


    }
}
